// @ts-check
import * as CANNON from 'cannon'
import Game from './Game'
import Engine from './Engine'
import Controls from './Controls'
import Map from './map/Map'
import Physics from './Physics'
import Player from './Player'

const factory = ({
  canvas,
  onNext,
  onPointerLockChange
}) => {
  const physics = new Physics()
  const world = physics.world

  // Create a sphere
  const  mass = 5
  const radius = 1.3
  const sphereShape = new CANNON.Sphere(radius)
  const sphereBody = new CANNON.Body({ mass: mass })
  sphereBody.addShape(sphereShape)
  sphereBody.position.set(0, 5, 0)
  sphereBody.linearDamping = 0.9
  world.addBody(sphereBody)

  const player = new Player({}, playerMesh => {
    playerMesh.scale.set(0.02, 0.02, 0.02)
    engine._scene.add(playerMesh)
    player.exec()
    engine.addSelf(player)
  })

  const controls = new Controls({
    onPointerLockChange,
    lockTriggerEl: canvas,
    cannonBody: sphereBody
  })

  const map = new Map({
    world
  })

  const engine = new Engine({
    canvas,
    map,
    onNext,
    controls,
    world
  })

  const game = new Game({
    engine,
    controls
  })

  return game
}

export default factory
